# Contributor: V.Krishn <vkrishn4@gmail.com>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=libwebsockets
pkgver=4.3.3
pkgrel=0
pkgdesc="C library for lightweight websocket clients and servers"
url="https://libwebsockets.org/"
arch="all"
license="MIT"
makedepends="cmake linux-headers openssl-dev>3 zlib-dev libuv-dev samurai"
subpackages="$pkgname-doc $pkgname-dev $pkgname-test:_test $pkgname-evlib_uv:_evlib"
source="$pkgname-$pkgver.tar.gz::https://github.com/warmcat/libwebsockets/archive/v$pkgver.tar.gz
	fix-version-string.patch
	"

build() {
	cmake -B build -G Ninja \
		-DCMAKE_BUILD_TYPE=MinSizeRel \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_VERBOSE_MAKEFILE=TRUE \
		-DLWS_IPV6=ON \
		-DLWS_WITH_LIBUV=ON \
		-DLWS_WITH_STATIC=OFF \
		-DLWS_LINK_TESTAPPS_DYNAMIC=ON \
		-DLWS_WITH_SDEVENT=OFF
	cmake --build build
}

# TODO: Use test subpkg to validate
check() {
	ctest --test-dir build --output-on-failure
}

package() {
	install -Dm644 LICENSE \
		"$pkgdir"/usr/share/licenses/$pkgname/LICENSE
	DESTDIR="$pkgdir" cmake --install build
}

_test() {
	pkgdesc="$pkgdesc (test server)"

	mkdir -p "$subpkgdir"/usr
	mv "$pkgdir"/usr/share "$pkgdir"/usr/bin "$subpkgdir"/usr/
}

_evlib() {
	local _plugin=${subpkgname#$pkgname-}
	pkgdesc="libwebsocket $_plugin plugin"
	amove usr/lib/libwebsockets-$_plugin.so
}

sha512sums="
2ffd248ddf283369725097ca7410f947fe0389c360b329c76f0754afab4ba87c20a0687c5e7b8bd991b157f9d20c6faa3049757b3398e66d08662c3aa7ff9658  libwebsockets-4.3.3.tar.gz
9325b7cadeb2ca8fb20b4759feb49fb255b1b14c432b7c8c3a1b793a49266bb4f69b78a9c72656039e94ba4fc59ca2c5e821ba33e4f133e72cab26786feb9d60  fix-version-string.patch
"
